# GerikSpellsPlotSelectScreen
# A screen that does drawing and other misc graphical chnages
# to the map for GerikSpells plot selection utility.
#
# There is no need to modify this file to use GerikSpells.
#

# gReloaded is used to determine when a python file reset has occured.
# After a python reaload, any time the screen tries to update, it will
# kill the screen. This is done because many times a screen will
# still be up and trying to update, which will cause problems because
# often global variables have not been reinitialized yet. Afterwards,
# gReloaded is set back to false.
gReloaded = True


from CvPythonExtensions import *
import CvUtil
import CvScreensInterface
import ScreenInput
import GerikSpellsInterface

gc = CyGlobalContext()

class GerikSpellsPlotSelectScreen:
    "GerikSpells Plot Select Screen"

    def __init__(self):
        self.m_bCancelOnNextUpdate = False
        self.m_pGraphicsPolicy = None
        self.m_pTargetPolicy = None
        

    # Called when a screen is first called up.
    def interfaceScreen(self, pGraphicsPolicy, pTargetPolicy):
        # If the screen was not up when the last python reload
        # happened, then gReloaded will be True until update, which
        # will be during a valid screen. This will stop that.
        global gReloaded
        gReloaded = False

        
        pScreen = CyGInterfaceScreen(GerikSpellsInterface.getPlotSelectScreenName(), GerikSpellsInterface.getPlotSelectScreenEnum() )
        self.m_pGraphicsPolicy = pGraphicsPolicy
        self.m_pTargetPolicy = pTargetPolicy

        iResX = pScreen.getXResolution()
        iResY = pScreen.getYResolution()
        
        # Used to make this screen the size of the entire screen. This way, all
        # inputs are caught and sent here.
        pScreen.setDimensions( 0, 0, iResX, iResY )

        # We'll handle the screen closing ourselves
        pScreen.setCloseOnEscape(False)

        # Finally, show the actual screen, and make sure to make the engine recognize that we will handle inputs.
        pScreen.showScreen(PopupStates.POPUPSTATE_IMMEDIATE, True)

        # Handle Input is only called if there is something to catch the input.
        # Make a huge, invisible checkbox to "catch" the input.
        pScreen.addCheckBoxGFC("CatchAllWidget", "Art/Interface/Buttons/empty.dds", "Art/Interface/Buttons/empty.dds",
                                    0, 0, iResX, iResY, WidgetTypes.WIDGET_GENERAL, 0, -1, ButtonStyles.BUTTON_STYLE_LABEL)

        self.m_bCancelOnNextUpdate = False



    def hideScreen(self):
        pScreen = CyGInterfaceScreen(GerikSpellsInterface.getPlotSelectScreenName(), GerikSpellsInterface.getPlotSelectScreenEnum() )
        pScreen.hideScreen()

    def mouseOverPlot(self):
        if (not self.m_bCancelOnNextUpdate):
            self.m_pGraphicsPolicy.mouseOverPlot()

    def stop(self, bCancel):
        if (not bCancel):
            self.hideScreen()
        else:
            self.m_bCancelOnNextUpdate = bCancel

    def handleInput(self, inputClass):
        if (not self.m_bCancelOnNextUpdate):
            # They pressed escape, so cancel the spell casting
            if (inputClass.getNotifyCode() == int(NotifyCode.NOTIFY_CHARACTER) and
                inputClass.getData() == int(InputTypes.KB_ESCAPE)):
                GerikSpellsInterface.cancelSpell()

            # They pressed escape, so cancel the spell casting
            if (inputClass.getNotifyCode() == int(NotifyCode.NOTIFY_CLICKED) and
                inputClass.getFlags() & MouseFlags.MOUSE_RBUTTONUP):
                GerikSpellsInterface.cancelSpell()

            if (inputClass.getNotifyCode() == int(NotifyCode.NOTIFY_CLICKED) and
                inputClass.getFlags() & MouseFlags.MOUSE_LBUTTONUP):
                self.m_pTargetPolicy.setTarget(CyInterface().getMouseOverPlot())
                self.m_pGraphicsPolicy.targetSelected(self.m_pTargetPolicy)
            

    def update(self, argsList):
        # Closes screens that may have been running during a python
        # file reload.
        global gReloaded
        if (gReloaded):
            screenToKill = CyGInterfaceScreen(GerikSpellsInterface.getPlotSelectScreenName(), GerikSpellsInterface.getPlotSelectScreenEnum() )
            screenToKill.hideScreen()
            gReloaded = False
            return

        if self.m_bCancelOnNextUpdate:
            self.hideScreen()
            self.m_bCancelOnNextUpdate = False
            GerikSpellsInterface.resetLocalInterfaceMode()
